package cn.yaboya.org.controller;

import cn.yaboya.basic.exception.BusinessException;
import cn.yaboya.basic.utils.ExcelUtil;
import cn.yaboya.org.domain.Employee;
import cn.yaboya.org.domain.ShopAuditLog;
import cn.yaboya.org.query.ShopQuery;
import cn.yaboya.org.service.IShopService;
import cn.yaboya.org.domain.Shop;
import cn.yaboya.basic.utils.AjaxResult;
import cn.yaboya.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@ResponseBody

@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }

    @PostMapping()
    public PageList<Shop> queryData(@RequestBody ShopQuery shopQuery){
        PageList<Shop> data = shopService.queryData(shopQuery);
        return data;
    }

    @PostMapping("/settlement")
    public AjaxResult settlement (@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult().me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new  AjaxResult().me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
     * @Title: pass
     * @Description: 店铺通过接口
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/7/2 16:51
     * @Parameters: [shopAuditLog]
     * @Return cn.yaboya.basic.utils.AjaxResult
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙请重试");
        }

    }

    /**
     * @Title: pass
     * @Description: 店铺驳回接口
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/7/2 16:51
     * @Parameters: [shopAuditLog]
     * @Return cn.yaboya.basic.utils.AjaxResult
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙请重试");
        }

    }

    /**
    * @Title: refuse
    * @Description: 店铺拒绝接口
    * @Author: LT
    * @Version: 1.0
    * @Date:  2022/7/2 23:05
    * @Parameters: [shopAuditLog]
    * @Return cn.yaboya.basic.utils.AjaxResult
    */
    @PostMapping("/audit/refuse")
    public AjaxResult refuse(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.refuse(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙请重试");
        }

    }


    @GetMapping("/export/excel")
    public void export(HttpServletResponse resp){
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "店铺信息.xlsx", resp);

    }

    @PostMapping("/import/excel")
    public void reject(@RequestPart MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shopService.insertBatchShop(shops);
    }

    @GetMapping("/echarts")
    public Map<String, Object> echarts(HttpServletResponse resp){

        return shopService.loadShopEchartsData();

    }

    @PostMapping("smsCode")
    public AjaxResult smsCode (@RequestBody Employee employee){
        try {
            System.out.println(employee);
            shopService.smsCode(employee);
            return new AjaxResult().me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult().me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new  AjaxResult().me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
